/**
 * 
 */
package com.jeecms.wicool.dao.killBuy.impl;

import org.hibernate.type.StringType;
import org.springframework.stereotype.Repository;

import com.google.common.base.Strings;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.killBuy.KillBuyDao;
import com.jeecms.wicool.entity.killBuy.KillBuy;

/**
 * @author spring
 * 
 */
@Repository
public class KillBuyDaoImpl extends HibernateBaseDao<KillBuy, Integer>
		implements KillBuyDao {

	@Override
	protected Class<KillBuy> getEntityClass() {
		return KillBuy.class;
	}

	@Override
	public KillBuy save(KillBuy bean) {
		Integer id = (Integer) getSession().save(bean);
		if (id > 0) {
			return bean;
		}
		return null;
	}

	@Override
	public KillBuy deleteById(Integer id) {
		KillBuy bean = get(id);
		if (bean != null) {
			getSession().delete(bean);
		}
		return bean;
	}

	@Override
	public KillBuy findById(Integer id) {
		return get(id);
	}

	@Override
	public Pagination getPage(Integer productId, String startTime,
			String endTime, int pageNo, int pageSize) {
		Finder f = Finder.create(" select bean from KillBuy bean where 1=1 ");
		if (productId != null) {
			f.append(" and bean.product.id=:product_id").setParam("product_id",
					productId);
		}
		if (!Strings.isNullOrEmpty(startTime)) {
			f.append(" and bean.startTime >= :startTime").setParam("startTime",
					startTime,new StringType());
		}
		if (!Strings.isNullOrEmpty(endTime)) {
			f.append(" and bean.endTime <= :endTime").setParam("endTime",
					endTime,new StringType());
		}

		f.append(" order by bean.updateTime desc");

		return find(f, pageNo, pageSize);
	}

}
